package cn.szzsi.job;

import cn.szzsi._Config;
import cn.szzsi.common.exception.BusinessException;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Orders;
import cn.szzsi.common.model.zshop.BusinessBalance;
import cn.szzsi.common.model.zshop.BusinessBalanceDetailService;
import cn.szzsi.common.model.zshop.BusinessBalanceService;
import cn.szzsi.service.AgentRecommendBusinessService;
import cn.szzsi.service.FenxiaoService;
import cn.szzsi.service.OrderService;
import com.jfinal.plugin.cron4j.ITask;
import org.apache.xpath.operations.Or;

import java.util.List;

/**
 * 已发货的订单超过10天,系统自动确认收货,并将订单进行分销,返现等处理
 */
public class OrderServiceTask implements ITask{

    @Override
    public void stop() {

    }

    @Override
    public void run() {
        List<Orders> ordersList=Orders.dao.find("SELECT * FROM `mall_orders` where paymentStatus=2 and shippingStatus=2 and orderStatus!=2 and (UNIX_TIMESTAMP(NOW())- UNIX_TIMESTAMP(createDate))<8*24*60*60");
        for(final Orders order:ordersList){
            _Config.fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {//执行分销处理
                    try {
                        Member member=Member.dao.findById(order.getMemberId());
                        FenxiaoService.service.fenxiaoHandle(member,order);//分销给推荐人
                        OrderService.service.productBackMoney(order,member);//购物返现
                        AgentRecommendBusinessService.service.orderBackMoneyToAgent(order);//给推荐代理商返现

                        BusinessBalanceService.service.updateBalance(order.getUid(),order.getTotalAmount());//给卖家充值
                        BusinessBalanceDetailService.service.saveInfo(order.getUid(),order.getTotalAmount(),1,order.getOrderSn(),order.getMemberId(),"会员确认收货");
                    } catch (BusinessException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
